package com.gouwudai.gobbs.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.extremecomponents.table.limit.Limit;

import com.gouwudai.gobbs.common.Constants;
import com.gouwudai.gobbs.common.IPageList;
import com.gouwudai.gobbs.dao.IMessageDAO;
import com.gouwudai.gobbs.pojo.TbbsMessage;
import com.gouwudai.gobbs.service.IMessageService;
import com.litt.core.util.ExtremeTableUtil;
import com.litt.core.util.ParamUtils;

public class MessageServiceImpl implements IMessageService
{
	IMessageDAO messageDAO;
	
	public void doReMessage(HashMap paramMap)
	{	
			TbbsMessage tbbsMessage = new TbbsMessage();
			tbbsMessage.setTitle(ParamUtils.getMapToString(paramMap, "title"));
			tbbsMessage.setContent(ParamUtils.getMapToString(paramMap, "content"));
			tbbsMessage.setMessageReId(ParamUtils.getMapToInt(paramMap, "messageReId"));
			
			int postFlag = ParamUtils.getMapToInt(paramMap, "postFlag");
			if(postFlag==0) //直接发送
			{				
				tbbsMessage.setSendFlag(0);
				tbbsMessage.setSendTime(new Date());
				tbbsMessage.setSendIp(ParamUtils.getMapToString(paramMap, "opIp"));
			}
			else if(postFlag==1) //发送同时保存到发件箱
			{				
				tbbsMessage.setSendFlag(1);	
				tbbsMessage.setSendTime(new Date());
				tbbsMessage.setSendIp(ParamUtils.getMapToString(paramMap, "opIp"));
			}
			else if(postFlag==2) //暂不发送保存到草稿箱
			{				
				tbbsMessage.setSendFlag(2);
			}
			
			tbbsMessage.setSendUser(ParamUtils.getMapToString(paramMap, "sendUser"));
			tbbsMessage.setTakeUser(ParamUtils.getMapToString(paramMap, "takeUser"));
			tbbsMessage.setOpIp(ParamUtils.getMapToString(paramMap, "opIp"));
			tbbsMessage.setOpTime(new Date());
			
			tbbsMessage.setReadFlag(false);
			messageDAO.save(tbbsMessage);
	}
	
	/**
	 * 保存新的短消息
	 * @param paramMap
	 * @return
	 */
	public void doNewMessage(HashMap paramMap)
	{	
			TbbsMessage tbbsMessage = new TbbsMessage();
			tbbsMessage.setTitle(ParamUtils.getMapToString(paramMap, "title"));
			tbbsMessage.setContent(ParamUtils.getMapToString(paramMap, "content"));
				
			int postFlag = ParamUtils.getMapToInt(paramMap, "postFlag");
			if(postFlag==0) //直接发送
			{				
				tbbsMessage.setSendFlag(0);
				tbbsMessage.setSendTime(new Date());
				tbbsMessage.setSendIp(ParamUtils.getMapToString(paramMap, "opIp"));
			}
			else if(postFlag==1) //发送同时保存到发件箱
			{				
				tbbsMessage.setSendFlag(1);	
				tbbsMessage.setSendTime(new Date());
				tbbsMessage.setSendIp(ParamUtils.getMapToString(paramMap, "opIp"));
			}
			else if(postFlag==2) //暂不发送保存到草稿箱
			{				
				tbbsMessage.setSendFlag(2);
			}
			
			tbbsMessage.setSendUser(ParamUtils.getMapToString(paramMap, "sendUser"));
			tbbsMessage.setTakeUser(ParamUtils.getMapToString(paramMap, "takeUser"));
			tbbsMessage.setOpIp(ParamUtils.getMapToString(paramMap, "opIp"));
			tbbsMessage.setOpTime(new Date());
			
			tbbsMessage.setReadFlag(false);
			messageDAO.save(tbbsMessage);
	}	
	
	/**
	 * 编辑草稿箱的短消息
	 * @param paramMap
	 * @return
	 */
	public void doEditDraft(HashMap paramMap)
	{	
			int messageId = ParamUtils.getMapToInt(paramMap, "messageId");

			TbbsMessage tbbsMessage = (TbbsMessage)messageDAO.load(messageId);
			
			tbbsMessage.setTitle(ParamUtils.getMapToString(paramMap, "title"));
			tbbsMessage.setContent(ParamUtils.getMapToString(paramMap, "content"));
			
			int postFlag = ParamUtils.getMapToInt(paramMap, "postFlag");
			if(postFlag==0) //直接发送
			{				
				tbbsMessage.setSendFlag(0);
				tbbsMessage.setSendTime(new Date());
				tbbsMessage.setSendIp(ParamUtils.getMapToString(paramMap, "opIp"));
			}
			else if(postFlag==1) //发送同时保存到发件箱
			{				
				tbbsMessage.setSendFlag(1);	
				tbbsMessage.setSendTime(new Date());
				tbbsMessage.setSendIp(ParamUtils.getMapToString(paramMap, "opIp"));
			}
			else if(postFlag==2) //暂不发送保存到草稿箱
			{				
				tbbsMessage.setSendFlag(2);
			}			
			tbbsMessage.setOpIp(ParamUtils.getMapToString(paramMap, "opIp"));
			tbbsMessage.setOpTime(new Date());

			messageDAO.update(tbbsMessage);
	}
	
	/**
	 * 直接发送当前点击的短消息
	 * @param paramMap
	 * @return
	 */
	public void doSendMessage(HashMap paramMap)
	{	
			int messageId = ParamUtils.getMapToInt(paramMap, "messageId");
			
			TbbsMessage tbbsMessage = (TbbsMessage)messageDAO.load(messageId);
			
			tbbsMessage.setSendFlag(1);
			tbbsMessage.setSendTime(new Date());
			tbbsMessage.setSendIp(ParamUtils.getMapToString(paramMap, "opIp"));
			tbbsMessage.setOpIp(ParamUtils.getMapToString(paramMap, "opIp"));
			tbbsMessage.setOpTime(new Date());

			messageDAO.update(tbbsMessage);
	}		

	public TbbsMessage doReadMessage(Integer id,String takeUser,String takeIp)
	{
		TbbsMessage tbbsMessage = messageDAO.load(id);
		//如果READ_FLAG为空的话则设置该标志为已读
		if(!tbbsMessage.getReadFlag().booleanValue())
		{	
			tbbsMessage.setReadFlag(true);
			tbbsMessage.setTakeUser(takeUser);
			tbbsMessage.setTakeIp(takeIp);
			tbbsMessage.setTakeTime(new Date());
			messageDAO.update(tbbsMessage);
		}
		return tbbsMessage;
	}
    
    public TbbsMessage load(Integer id)
    {
        return messageDAO.load(id);       
    }
	
	public void save(TbbsMessage tbbsMessage)
	{ 
		messageDAO.save(tbbsMessage);		
	}

	public void update(TbbsMessage tbbsMessage)
	{
			messageDAO.update(tbbsMessage);
	}

	public void delete(String ids)
	{
			messageDAO.delete(ids);
	}

	public IPageList listDraft(String loginId,int pageIndex,int pageSize)
	{		
		return messageDAO.listDraft(loginId,pageIndex, pageSize);
	}
	
	public IPageList listSend(String loginId,int pageIndex,int pageSize)
	{		
		return messageDAO.listSend(loginId,pageIndex, pageSize);
	}

	public IPageList listTake(String loginId,int pageIndex,int pageSize)
	{		
		return messageDAO.listTake(loginId,pageIndex, pageSize);
	}
	
	public List listNew(String loginId)
	{
		return messageDAO.listNew(loginId);
	}
	
	
	public void listTakeTable(String loginId,javax.servlet.http.HttpServletRequest request)
	{
		Limit limit = ExtremeTableUtil.getLimit(request, Constants.DEFAULT_PAGE_SIZE);
        IPageList page = this.listTake(loginId,limit.getPage(), limit.getCurrentRowsDisplayed());
		request.setAttribute("resultList",page.getRsList());
		request.setAttribute("totalRows",page.getTotalSize());
	}
	
	public void listSendTable(String loginId,javax.servlet.http.HttpServletRequest request)
	{
		Limit limit = ExtremeTableUtil.getLimit(request, Constants.DEFAULT_PAGE_SIZE);
        IPageList page = this.listSend(loginId,limit.getPage(), limit.getCurrentRowsDisplayed());
		request.setAttribute("resultList",page.getRsList());
		request.setAttribute("totalRows",page.getTotalSize());
	}	
	
	public void listDraftTable(String loginId,javax.servlet.http.HttpServletRequest request)
	{
		Limit limit = ExtremeTableUtil.getLimit(request, Constants.DEFAULT_PAGE_SIZE);
        IPageList page = this.listDraft(loginId,limit.getPage(), limit.getCurrentRowsDisplayed());
		request.setAttribute("resultList",page.getRsList());
		request.setAttribute("totalRows",page.getTotalSize());
	}

	public void setMessageDAO(IMessageDAO messageDAO) {
		this.messageDAO = messageDAO;
	}	
	
}